package com.wondream.java.io.example;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class FileInputStreamTest {

    private final Log logger = LogFactory.getLog(FileInputStreamTest.class);

    public static void main(String[] args) {

        File file = null;

        FileInputStream fileInputStream = null;

        try{
            // 读取文件方式一 spring中工具类
            file = ResourceUtils.getFile("classpath:application.properties");

            fileInputStream = new FileInputStream(file);

//            // 根据输入流可用大小 定义缓冲区
//            byte[] data = new byte[fileInputStream.available()];
//
//            // 读取数据到字节数组
//            fileInputStream.read(data);
//
//            String var1 = new String(data, "UTF-8");
//
//            logger.info(var1);

            StringBuilder stringBuilder = new StringBuilder();
            byte[] data = new byte[20];
            Arrays.fill(data, (byte) 0);
            System.out.println(new String(data, StandardCharsets.UTF_8).trim()); // 空串
            int readLen = 0;
            while((readLen = fileInputStream.read(data))>0){
//                stringBuilder.append(new String(data, 0, readLen));
                stringBuilder.append(new String(data, StandardCharsets.UTF_8).trim());
                Arrays.fill(data, (byte) 0);
            }
            System.out.println(stringBuilder);;
            // 用完关闭流
            fileInputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
